from sqlalchemy.orm import Session
from app import models, schemas
from datetime import datetime

def get_conversation(db: Session, conversation_id: int):
    return db.query(models.Conversation).filter(models.Conversation.id == conversation_id).first()

def get_conversations_by_user(db: Session, user_id: int, skip: int = 0, limit: int = 100):
    return db.query(models.Conversation)\
        .filter(models.Conversation.user_id == user_id)\
        .offset(skip)\
        .limit(limit)\
        .all()

def create_conversation(db: Session, conversation: schemas.ConversationCreate):
    db_conversation = models.Conversation(
        user_id=conversation.user_id,
        question=conversation.question,
        answer=conversation.answer
    )
    db.add(db_conversation)
    db.commit()
    db.refresh(db_conversation)
    return db_conversation

def delete_conversation(db: Session, conversation_id: int):
    db.query(models.Conversation).filter(models.Conversation.id == conversation_id).delete()
    db.commit()